package com.example.listview;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.example.Utils.CommonUrl;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.*;

public class MainActivity extends Activity {
    private ListView listView_dishName;
    private ListView listView_dishKind;

    private ProgressDialog dialog;
    private DishNameAdapter dishNameAdapter;
    private DishKindAdapter dishKindAdapter;

    private List<Map<String,Object>> requestList = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView_dishName = (ListView) findViewById(R.id.listView_dishName);
        listView_dishKind = (ListView) findViewById(R.id.listView_dishKind);
        dialog = new ProgressDialog(this);
        dishNameAdapter = new DishNameAdapter(MainActivity.this);//构建适配器
        dishKindAdapter = new DishKindAdapter(MainActivity.this);

        dialog.setTitle("提示");
        dialog.setMessage("正在下载，请稍后");

        new MyTask_DishName().execute(CommonUrl.PRODUCT_URL);
    }
    public class MyTask_DishName extends AsyncTask<String,Void,List<Map<String,Object>>>{
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog.show();
        }

        @Override
        protected void onPostExecute(List<Map<String, Object>> result) {
            super.onPostExecute(result);


            dishNameAdapter.setData(result);
            listView_dishName.setAdapter(dishNameAdapter);
            dishNameAdapter.notifyDataSetChanged();

            requestList = result;

            dialog.dismiss();
        }

        @Override
        protected List<Map<String, Object>> doInBackground(String... strings) {
            List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
            //连接网络，获取json数据并进行解析
            try{
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(strings[0]);
                HttpResponse httpResponse = httpClient.execute(httpPost);
                if(httpResponse.getStatusLine().getStatusCode()==200){
                    String jsonString = EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
                    JSONObject jsonObject = new JSONObject(jsonString);
                    JSONArray dish_list = jsonObject.getJSONArray("dish_list");
                    for(int i = 0;i<dish_list.length();i++){
                        JSONObject dish_list_count = dish_list.getJSONObject(i);
                        Map<String,Object> map = new HashMap<String, Object>();
                        //迭代输出json 的key 作为map 的key
                        Iterator<String> iterator = dish_list_count.keys();
                        while (iterator.hasNext()){
                            String dish_list_key = iterator.next();
                            Object dish_list_value = dish_list_count.get(dish_list_key);
                            map.put(dish_list_key,dish_list_value);
                        }
                        list.add(map);
                    }

                }

            }
            catch (Exception e){
                e.printStackTrace();
            }

            return list;
        }
    }

    public class DishNameAdapter extends BaseAdapter{
        private Context context;
        private LayoutInflater layoutInflater;
        private List<Map<String,Object>> list = null;
        public DishNameAdapter(Context context){
            this.context = context;
            layoutInflater = layoutInflater.from(context);
        }
        public void setData(List<Map<String,Object>> list){
            this.list = list;
        }
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {
            View view = null;
            if(convertView == null){
                view = layoutInflater.inflate(R.layout.item_dishname,null);

            }else {
                view = convertView;
            }
            TextView textView_dishName = (TextView) view.findViewById(R.id.textView_dishName);
            System.out.println(list);
            textView_dishName.setText(list.get(i).get("dish_name").toString());
            /* 获得 dish_kind_name
            try{
                JSONArray jsonArray = (JSONArray)list.get(0).get("dish_kind");
                JSONObject dish_kind_count = jsonArray.getJSONObject(0);
                textView_dishName.setText(dish_kind_count.get("dish_kind_name").toString());
            }catch (Exception e){
                e.printStackTrace();
            }*/

            return view;
        }
    }

    public class DishKindAdapter extends BaseAdapter{
        private Context context;
        private LayoutInflater layoutInflater;
        private List<Map<String,Object>> list = null;
        public DishKindAdapter(Context context){
            this.context = context;
            layoutInflater = layoutInflater.from(context);
        }
        public void setData(List<Map<String,Object>> list){
            this.list = list;
        }
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {
            View view = null;
            if(convertView == null){
                view = layoutInflater.inflate(R.layout.item_dishkind,null);

            }else {
                view = convertView;
            }
            TextView textView_dishKind = (TextView) view.findViewById(R.id.textView_dishKind);
            System.out.println(list);
            textView_dishKind.setText(list.get(i).get("dish_kind_name").toString());

            return view;
        }
    }
    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        listView_dishName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String itemNum  = String.valueOf(i);
                //Toast.makeText(MainActivity.this,itemNum + requestString,Toast.LENGTH_SHORT).show();
                new MyTask_DishKind().execute(itemNum,requestList);


            }
        });
    }
    public class MyTask_DishKind extends AsyncTask<Object,Void,List<Map<String,Object>>>{
        @Override
        protected List<Map<String, Object>> doInBackground(Object... objects) {
            List<Map<String,Object>> list1;
            List<Map<String,Object>> list2 = new ArrayList<Map<String, Object>>();
            list1 = (List<Map<String,Object>>)objects[1];
            try{
                JSONArray dish_kind_count = (JSONArray)list1.get(Integer.parseInt(objects[0].toString())).get("dish_kind");
                for(int j = 0;j<dish_kind_count.length();j++){
                    JSONObject dish_kind = dish_kind_count.getJSONObject(j);
                    Map<String,Object> map = new HashMap<String, Object>();
                    //迭代输出json 的key 作为map 的key
                    Iterator<String> iterator = dish_kind.keys();
                    while (iterator.hasNext()){
                        String dish_kind_key = iterator.next();
                        Object dish_kind_value = dish_kind.get(dish_kind_key);
                        map.put(dish_kind_key,dish_kind_value);
                    }
                    list2.add(map);
                    //System.out.println(list);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return list2;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(List<Map<String, Object>> list2) {
            super.onPostExecute(list2);

            dishKindAdapter.setData(list2);
            listView_dishKind.setAdapter(dishKindAdapter);
            dishKindAdapter.notifyDataSetChanged();
        }


    }
}
